package com.jjb.common.ability.adapter.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class BeanUtil {

    public static <T> Class<T> getInterfacesByType(Class targetClass, Class<T> zclass) {

        Class<?>[] zclas = targetClass.getInterfaces();
        Class<T> result = null;
        if( zclas != null ) {

            for( Class z :  zclas) {
                if ( z.equals(zclass) ) {
                    result = zclass;
                    break;
                } else {
                    Class<T> res = getInterfacesByType(z, zclass);
                    if( res != null ) {
                        result = res;
                        break;
                    }
                }
            }
        }
        return result;
    }

    public static Type[]  getGenericity(Object target,Class genClass) throws Exception{
        Type[] types = target.getClass().getGenericInterfaces();
        Type[] result = null;

        for( Type type : types ) {
            String  typeName = type.getTypeName();
            if( genClass.isAssignableFrom((Class)type) ) {
                Type[] ts = ((Class<?>) type).getGenericInterfaces();
                if (ts != null) {
                    for (Type t : ts) {
                        if (t instanceof ParameterizedType) {
                            result = ((ParameterizedType) t).getActualTypeArguments();
                            break;
                        }
                    }
                }
            }
        }

        return result;
    }

    public static void main(String[] args) throws Exception{
        Map<String, Object> res = new HashMap<>();
        Class a = getInterfacesByType(res.getClass(), Map.class);
        System.out.println(a);

        Type[] zclass = getGenericity(res, java.util.Map.class);
        System.out.println(zclass);
    }

}
